/*
 * @Author: your name
 * @Date: 2021-04-10 13:41:12
 * @LastEditTime: 2021-04-20 18:13:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\reserveMod\controller.go
 */
package reserveMod

import (
	"chronic/module/userMod"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func getReserveHistory(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	resvs, err := findReservationsByPatientId(user.ID)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, resvs)
	}
}

type reserveDto struct {
	PatientName   string `form:"patientName" json:"patientName" binding:"required"`
	PatientNumber string `form:"patientNumber" json:"patientNumber" binding:"required"`
	PatientTel    string `form:"patientTel" json:"patientTel" binding:"required"`
	DoctorID      string `form:"doctorId" json:"doctorId" binding:"required"`
	ReserveTime   string `form:"reserveTime" json:"reserveTime" binding:"required"`
}

func postOneReserve(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	var resv *Reserve
	var err error
	var input reserveDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	resv, err = addOneReserve(user.ID, input)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		resq, err := updataReserveQuota(resv)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		} else {
			ctx.JSON(http.StatusOK, resq)
		}
	}

}

func getTimeReserve(ctx *gin.Context) {
	time := ctx.Query("reserveTime")
	resq, err := findEnableDoctors(time)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
		return
	}

	ctx.JSON(http.StatusOK, resq)
}

// func getBanReserve(ctx *gin.Context) {
// 	time := ctx.Query("reserveTime")
// 	banResqs, _ := findBanReserveDoctors(time)
// 	ctx.JSON(http.StatusOK, banResqs)

// }

type reserveQuotaDto struct {
	AnyDuration    string `form:"anyDuration" json:"anyDuration" binding:"required"`
	TheDurationMax int    `form:"theDurationMax" json:"theDurationMax" binding:"required"`
}

func postReserveQuota(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	var input reserveQuotaDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	resq, err := addReserveQuota(user.ID, input)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusOK, resq)
}

// PlanA：
// getBanReserve得到的是在reserveTime时间段下已经约满了的医生列表banResqs，
// 将这些医生返回给前端，前端首先应获取到全部医生，
// 每一个医生为一个listtile，判断该医生的id是否出现在banResqs中，
// 若出现，则将onTap事件设置为不可预约；否则便是可预约

// PlanB：
// 每个医生在某一时间段是否可预约，以及可预约名额均由医生手动添加，
// 修改ReserveQuota表的结构，包括医生id、某一时间段、可约人数、已约人数
// 后端得到预约时间后，在ReserveQuota表中查找该预约时间下 已约人数 < 可约人数 的数据
// 即为在该时段下可预约的医生，将其返回给前端。

func getAllReserves(ctx *gin.Context) {
	reserves, err := findAllReserves()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, reserves)
	}
}

func getReservesByUserId(ctx *gin.Context) {
	id := ctx.Query("userId")
	userId, _ := strconv.ParseUint(id, 10, 64)
	reserves, err := findReservesByUserId(uint(userId))
	if err != nil {
		ctx.JSON(http.StatusNotFound, "未找到！！")
	} else {
		ctx.JSON(http.StatusOK, reserves)
	}
}

func getReservesByPatient(ctx *gin.Context) {
	condition := ctx.Query("condition")
	reserves, err := findReservesByPatient(condition)
	if err != nil {
		ctx.JSON(http.StatusNotFound, "未找到！！")
	} else {
		ctx.JSON(http.StatusOK, reserves)
	}

}

func getReservesByDoctor(ctx *gin.Context) {
	condition := ctx.Query("condition")
	reserves, err := findReservesByDoctor(condition)
	if err != nil {
		ctx.JSON(http.StatusNotFound, "未找到！！")
	} else {
		ctx.JSON(http.StatusOK, reserves)
	}

}

func deleteReserve(ctx *gin.Context) {
	reserveId := ctx.Query("id")
	id, _ := strconv.ParseUint(reserveId, 10, 64)
	err := deleteReserveById(uint(id))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, "删除成功！！")
	}

}
